package com.example.d10_homework.homework01;

import java.io.*;
import java.net.*;

public class FileClient {
    private static final String SERVER_IP = "127.0.0.1"; // 本地测试用
    private static final int SERVER_PORT = 12345;

    public static void main(String[] args) {
        try (
                Socket socket = new Socket(SERVER_IP, SERVER_PORT);
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedReader fileReader = new BufferedReader(new FileReader("local_file.txt"))
        ) {
            String serverResponse;
            while ((serverResponse = in.readLine()) != null) {
                System.out.println("服务器: " + serverResponse);
                break;
            }

            String line;
            while ((line = fileReader.readLine()) != null) {
                out.println(line);
            }
            out.println("END"); // 发送结束标记

            while ((serverResponse = in.readLine()) != null) {
                System.out.println("服务器: " + serverResponse);
                break;
            }
        } catch (UnknownHostException e) {
            System.err.println("未知主机: " + SERVER_IP);
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}